package com.example.librarymanagementsystem.Controller;


import com.example.librarymanagementsystem.model.*;
import com.example.librarymanagementsystem.severice.BookSeverice;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookSeverice bookSeverice;

    //    添加图书
    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        //校验参数
        log.info("接收到参数:{}", bookInfo);

        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null) {
            return "参数不能为空";
        }
        //添加图书
        try {
            Integer result = bookSeverice.insertBook(bookInfo);
            if (result > 0) {
                return "";
            }
        } catch (Exception e) {
            log.error("添加图书异常，异常信息：" + e);
        }
        return "添加错误";
    }


    //查询图书列表
    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session) {
        //从session中获取用户信息
        //如果用户信息为空，说明用户未登录，
   /*     UserInfo userSessionKey = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
        if(userSessionKey == null||userSessionKey.getId()<=0){
            return Result.noLogin();
        }*/
        PageResult<BookInfo> bookList = bookSeverice.getBookListByPage(pageRequest);
        return Result.success(bookList);
    }

    //根据id查询图书
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("根据id查询图书信息，id:" + bookId);
        return bookSeverice.queryBookById(bookId);
    }
    /**
    *更新图书
    */
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书，bookInfo:{}",bookInfo);
        try{
            Integer result = bookSeverice.updateBookById(bookInfo);
            if(result>0){
                return "";
            }
            return "内部错误，请联系管理员";
        } catch (Exception e) {
            log.error("更新图书失败 e:"+e);
            return "更新图书失败";
        }
    }

    /**
     * 逻辑删除
     * @param bookId
     * @return
     */
    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId){
        log.info("删除图书：",+bookId);
        try{
            Integer result = bookSeverice.deleteBookById(bookId);
            if(result>0){
                return "";
            }
            return "内部错误，请联系管理员";
        } catch (Exception e) {
            log.error("更新图书失败 e:"+e);
            return "更新图书失败";
        }
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/batchDeleteBook",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("接收到参数，ids:{}",ids);
        Integer result = bookSeverice.batchDeleteById(ids);
        if(result > 0){
            return "";
        }
        return "删除失败，请联系管理员";
    }
}
